package com.sxt.servlet;

import cn.hutool.core.util.StrUtil;
import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.pojo.User;
import com.sxt.service.ICustomerService;
import com.sxt.service.impl.CustomerServiceImpl;
import com.sxt.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author: Mr.T
 * @date 2020-11-03 11:50
 */
@WebServlet("/customer.do")
public class CustomerServlet extends HttpServlet {

    ICustomerService customerService = new CustomerServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String service = req.getParameter("service");
        try {
            //此时 service 值与方法名相同  则可以根据service找到方法
            Method method = this.getClass().getDeclaredMethod(service,HttpServletRequest.class, HttpServletResponse.class);
            //进行方法调用
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            throw  new RuntimeException("找不到方法:"+service);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询客户列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //页码
        String page = req.getParameter("page");
        //每页显示的数据条数
        String limit = req.getParameter("limit");
        //获取查询的参数
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String company = req.getParameter("company");
        String deleted = req.getParameter("deleted");
        String userId = req.getParameter("userId");
        //查询参数
        Map<String,String> params = new HashMap<>();
        //校验参数 放入查询的容器
        if (StrUtil.isNotBlank(name)){
            params.put("name",name);
        }
        if (StrUtil.isNotBlank(phone)){
            params.put("phone",phone);
        }
        if (StrUtil.isNotBlank(sex)){
            params.put("sex",sex);
        }
        if (StrUtil.isNotBlank(company)){
            params.put("company",company);
        }
        if (StrUtil.isNotBlank(deleted)){
            params.put("deleted",deleted);
        }
        if (StrUtil.isNotBlank(userId)){
            params.put("userId",userId);
        }
        // 获取分页查询的数据
        Result result = customerService.queryPage(params, page, limit);
        //将数据返回
        RespUtil.writer(result,resp);
    }

    /**
     * 新增客户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //页码
        //每页显示的数据条数
        //获取查询的参数
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String company = req.getParameter("company");
        HttpSession session = req.getSession();
        Object o = session.getAttribute(Constant.SESSION_CURRENT_USER_KEY);
        User user = (User) o;
        Integer id = user.getId();
        Result result = customerService.add(name, phone, sex,company,id);
        //将数据返回
        RespUtil.writer(result,resp);
    }

    /**
     * 修改客户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String company = req.getParameter("company");
        Result result = customerService.update(id,name, phone, sex,company);
        //将数据返回
        RespUtil.writer(result,resp);
    }

    /**
     * 删除客户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Result result = customerService.delete(id);
        //将数据返回
        RespUtil.writer(result,resp);
    }


    /**
     * 设置客户的业务员ID
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void setSaleman(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        String[] cIds = req.getParameterValues("cId");
        Result result = customerService.setSaleman(userId,cIds);
        //将数据返回
        RespUtil.writer(result,resp);
    }





}
